package cn.iocoder.yudao.module.call.controller.admin.ivrkeywordscustom;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.ivrkeywordscustom.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrkeywordscustom.IvrKeywordsCustomDO;
import cn.iocoder.yudao.module.call.service.ivrkeywordscustom.IvrKeywordsCustomService;

@Tag(name = "管理后台 - ivr流程关键字")
@RestController
@RequestMapping("/call/ivr-keywords-custom")
@Validated
public class IvrKeywordsCustomController {

    @Resource
    private IvrKeywordsCustomService ivrKeywordsCustomService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr流程关键字")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrKeywordsCustom(@Valid @RequestBody IvrKeywordsCustomSaveReqVO createReqVO) {
        return success(ivrKeywordsCustomService.createIvrKeywordsCustom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr流程关键字")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrKeywordsCustom(@Valid @RequestBody IvrKeywordsCustomSaveReqVO updateReqVO) {
        ivrKeywordsCustomService.updateIvrKeywordsCustom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr流程关键字")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrKeywordsCustom(@RequestParam("id") Long id) {
        ivrKeywordsCustomService.deleteIvrKeywordsCustom(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr流程关键字")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrKeywordsCustomRespVO> getIvrKeywordsCustom(@RequestParam("id") Long id) {
        IvrKeywordsCustomDO ivrKeywordsCustom = ivrKeywordsCustomService.getIvrKeywordsCustom(id);
        return success(BeanUtils.toBean(ivrKeywordsCustom, IvrKeywordsCustomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr流程关键字分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrKeywordsCustomRespVO>> getIvrKeywordsCustomPage(@Valid IvrKeywordsCustomPageReqVO pageReqVO) {
        PageResult<IvrKeywordsCustomDO> pageResult = ivrKeywordsCustomService.getIvrKeywordsCustomPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrKeywordsCustomRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得ivr流程关键字分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrKeywordsCustomRespVO>> simpleList(@Valid IvrKeywordsCustomPageReqVO pageReqVO) {
        List<IvrKeywordsCustomDO> pageResult = ivrKeywordsCustomService.simpleList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrKeywordsCustomRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr流程关键字 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrKeywordsCustomExcel(@Valid IvrKeywordsCustomPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrKeywordsCustomDO> list = ivrKeywordsCustomService.getIvrKeywordsCustomPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr流程关键字.xls", "数据", IvrKeywordsCustomRespVO.class,
                        BeanUtils.toBean(list, IvrKeywordsCustomRespVO.class));
    }

}